package aop.introduction.annotation;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class UsageTracking {

	private static final Logger log = LoggerFactory.getLogger(UsageTracking.class);
	
	@DeclareParents(value = "aop.introduction.annotation.InfoServiceImpl", defaultImpl = DefaultUsageTracked.class)
	public IUsageTracked mixin;

	/**
	 * Every method calling for InfoServiceImpl would trigger the counter.
	 * @param usageTracked
	 */
	@Before("aop.introduction.annotation.PointCuts.infoService() && this(usageTracked)")
	public void recordUsage(IUsageTracked usageTracked) {
		usageTracked.incrementUseCount();
		log.debug("InfoService was used " + usageTracked.getCount() + " times.");
	}
}
